//
//  outBottomView.m
//  AirQuality
//
//  Created by Mac on 16/1/19.
//  Copyright © 2016年 Po. All rights reserved.
//

#import "outBottomView.h"
#import "outBottomTableViewCell.h"


@interface outBottomView ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSString * key;
@property (strong, nonatomic) NSMutableArray * allStation, * displayRowArray;
@property (retain, nonatomic) UITableView * tableView;      //城市信息列表

@end

@implementation outBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self initInterface];
        _key = @"AQI";
    }
    return self;
}

- (void)initData
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OutpositionValueIsGet:) name:@"OutPositionValueIsGet" object:nil];
}

- (void)initInterface
{
     [self addSubview:[self getTableView]];
}
- (UITableView *)getTableView
{
    if (!_tableView) {
        CGRect rect = self.bounds;
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
    return _tableView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString * cellIdent = @"outBottomTableViewCell";
    outBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"outBottomTableViewCell" owner:self options:nil];
        cell = nib[0];
        [cell.contentView setBackgroundColor:[UIColor clearColor]];
        [cell.selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    NSDictionary * keyDic = @{@"SO₂":@"SO2",
                              @"NO₂":@"NO2",
                              @"CO":@"CO",
                              @"O₃":@"O3",
                              @"PM":@"PM2_5",
                              @"PM₁₀":@"PM10",
                              @"AQI":@"AQI"};
    NSDictionary * dic = _displayRowArray[indexPath.row][0];
    NSString * key = keyDic[_key];
    NSString * aqiValue = dic[key];
    cell.stationName.text = _allStation[indexPath.row];
    [cell setDataValue:aqiValue type:_key withColNum:aqiValue];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_delegate) {
        NSDictionary * dic = _displayRowArray[indexPath.row][0];
        [_delegate skipNextDetailPage:dic];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allStation.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0f;
}

- (void)changeTypeKey:(NSString *)key
{
    _key = key;
    [_tableView reloadData];
}
#pragma mark - notification
- (void)OutpositionValueIsGet:(NSNotification *)data
{
    NSDictionary * dic = data.object;
    if (!dic) {
        return;
    }
    _allStation = [NSMutableArray array];
    _displayRowArray = [NSMutableArray array];
    for (NSDictionary * columns  in dic) {
        NSDictionary * comlumn = columns[@"columns"];
        [_allStation addObject:comlumn[@"STATIONNAME"]];
        [_displayRowArray addObject:[columns allValues]];
    }
    [_tableView reloadData];
}

- (void)removeNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OutPositionValueIsGet" object:nil];
}
@end
